var http = require('http');

module.exports = function (query, credentials, cb) {
    var options = {
        hostname: 'api.map.baidu.com',
        path: '/geocoder/v2/?address=' + encodeURIComponent(query) + '&output=json&ak=' + credentials.geocode.key
    };
    var req = http.request(options, function (res) {
        var data = '';
        res.on('data', function (chunk) {
            data += chunk;
        });
        res.on('end', function () {
            data = JSON.parse(data);
            if (data.result) {
                cb(null, data.result.location);
            } else {
            }
        });
    });

    req.on('error', function (e) {
        cb(e.message, null);
    });

    req.end();
};
